package com.aw.guiguzi.module.setting.adapter

import android.widget.Switch
import com.aw.baselib.util.AwLog
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.CalSuggestItemBean
import com.aw.guiguzi.event.EventRefreshCalResultType
import com.aw.guiguzi.listeners.OnCommonCheckedListener
import com.aw.guiguzi.util.AppCacheUtil
import com.aw.guiguzi.util.AppUserUtil.isAdmin
import com.aw.guiguzi.util.CommonUtil.setOnCheckedChangeListener
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import org.greenrobot.eventbus.EventBus

class SettingUiSuggestAdapter :
    BaseQuickAdapter<CalSuggestItemBean, BaseViewHolder>(R.layout.adapter_setting_ui_suggest) {
    private var mList: MutableList<CalSuggestItemBean> = ArrayList()

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    override fun convert(helper: BaseViewHolder, bean: CalSuggestItemBean) {
        helper.setText(R.id.tv_type, bean.type)
            .setText(R.id.tv_name, "（" + bean.name + "）")
        val switch_view = helper.getView<Switch>(R.id.switch_view)
        switch_view.isChecked = bean.isEnable
        if (!isAdmin) {
            switch_view.isEnabled = false
        }
        setOnCheckedChangeListener(switch_view, object : OnCommonCheckedListener {
            override fun callback(isChecked: Boolean) {
                bean.isEnable = isChecked
                AppCacheUtil.setTrendActive(bean.tag, isChecked)
                AwLog.d("SettingUiSuggestAdapter, tag: " + bean.tag + " ,isChecked: " + isChecked)
                EventBus.getDefault().postSticky(EventRefreshCalResultType(null))
            }
        })
    }

    fun addAllData(dataList: MutableList<CalSuggestItemBean>) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }
}